﻿using System;
using System.Globalization;
using System.IO;
using Avalonia.Data.Converters;
using Avalonia.Media.Imaging;
using Avalonia.Platform;
using MinecraftEnchantCalculator.Core;
using MinecraftEnchantCalculator.Models;

namespace MinecraftEnchantCalculator.Converters.Xaml;

public class EnchantableMetadataConverter : IValueConverter
{
  public static readonly Uri EnchantedBookIconUri = new("avares://MinecraftEnchantCalculator/Assets/EnchantedBook.png");

  public object? Convert(
    object? value, Type targetType, object? parameter, CultureInfo culture
  )
  {
    switch (value) {
      case EnchantItem item when parameter is "name": return item.ItemEntity.Name;
      case EnchantItem item when parameter is "icon": {
        using var ms = new MemoryStream(item.ItemEntity.IconBytes);
        return new Bitmap(ms);
      }
      case EnchantBook when parameter is "name": return "附魔书";
      case EnchantBook when parameter is "icon": {
        using var stream = AssetLoader.Open(EnchantedBookIconUri);
        return new Bitmap(stream);
      }
    }

    return null;
  }

  public object? ConvertBack(
    object? value, Type targetType, object? parameter, CultureInfo culture
  )
  {
    throw new NotImplementedException();
  }
}